package com.wyd.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.wyd.pojo.Clazz;
import com.wyd.pojo.User;
import com.wyd.utils.DBUtil;
import com.wyd.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@RestController
public class UserController {

    @RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")
//    @ResponseBody //不会走视图解析器，会直接返回一个字符串
    public String json1() throws JsonProcessingException {

        //jackson,ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        //创建一个对象
        User user = new User("于全",3,"男");
        String str =  mapper.writeValueAsString(user);

        return str;
    }

    @RequestMapping("/j2")
    public String json2() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User("于全",3,"男");
        String str = mapper.writeValueAsString(user);
        return str;
    }

    @RequestMapping("/j3")
    public String json3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        for (int i = 1;i<=10;i++){//执行10次
            System.out.println(i);
        }
        for(int i = 1;i<10;i++){//执行9次
            System.out.println(i);
        }
        ArrayList<User> list = new ArrayList<User>();
        User user1 = new User("于全",1,"男");
        User user2 = new User("于全",2,"男");
        User user3 = new User("于全",3,"男");
        User user4 = new User("于全",4,"男");
        User user5 = new User("于全",5,"男");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        String str = mapper.writeValueAsString(list);
        return str;
    }

    @RequestMapping("/j4")
    public String json4() throws JsonProcessingException {
        DBUtil dbUtil = new DBUtil();

        ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String, Object>>();
        list = dbUtil.queryForList("select c.gradename,c.majorname,c.clazzname from clazz c");
        String gradename = null;
        String majorname = null;
        String clazzname = null;
        for(HashMap<String,Object> map : list){
            gradename = map.get("gradename").toString();
            majorname = map.get("majorname").toString();
            clazzname = map.get("clazzname").toString();
        }
        ArrayList<Clazz> list1 = new ArrayList();
        Clazz clazz1 = new Clazz(gradename,majorname,clazzname);
        list1.add(clazz1);
        Clazz clazz2 = new Clazz(gradename,majorname,clazzname);
        list1.add(clazz2);
        System.out.println(list1);
        String str = gradename  +majorname+clazzname;
        String str1 = JsonUtils.getJson(list1);
        return str1;
    }

    @RequestMapping("/j5")
    public String json5() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
//        System.out.println(sdf.format(date));
        //使用ObjecMapper 来
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        mapper.setDateFormat(sdf);
        return mapper.writeValueAsString(/*sdf.format(date)*/date);
    }

    @RequestMapping("/j6")
    public String json6(){
        Date date = new Date();
        return JsonUtils.getDatetoJson(date,"yyyy-MM-dd  HH:mm:ss");
    }
}
